package com.hcbd.tdwt.ui.setting.hlct;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.BeidouBoxParams;

import com.pangu.bdsdk2021.entity.extensionterminal.DlYxxInfo;
import com.pangu.bdsdk2021.terminal.TerminalSendManager;

import org.androidannotations.annotations.AfterTextChange;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_bluetooth_name_set)
public class BluetoothNameSetActivity extends BaseActivity {
    @ViewById
    Button implementBtn;
    @ViewById
    EditText bluetoothName;
    @ViewById
    TextView title,leftTv;
    @ViewById
    LinearLayout bgLL;
    @ViewById
    ImageButton headLeft;
    @AfterViews
    protected void init() {
        initMsgRegister();
        bluetoothName.setText(BeidouBoxParams.blueName);
        bluetoothName.setTextColor(ContextCompat.getColor(this,R.color.black));
        title.setText("蓝牙名称设置");
        bgLL.setBackground(ContextCompat.getDrawable(this,R.color.white));
        title.setTextColor(ContextCompat.getColor(this,R.color.black));
        headLeft.setBackground(ContextCompat.getDrawable(this,R.drawable.left_arrow));
        leftTv.setTextColor(ContextCompat.getColor(this,R.color.black));
        implementBtn.setBackground(ContextCompat.getDrawable(this,R.drawable.btn_light_blue_cilck_bg));
        setAndroidNativeLightStatusBar(true);
        if(BeidouBoxParams.isBoxConnectNormal){
            TerminalSendManager.getInstance().sendFlyMc(0, "");
        }else{
            toast("未连接蓝牙,无法使用该功能!");
        }
    }

    @AfterTextChange(R.id.bluetoothName)
    void bluetoothName(TextView tv, Editable s) {
        if (s.length() > 0) {
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c >= 0x4e00 && c <= 0X9fff) { // 根据字节码判断
                    // 如果是中文，则清除输入的字符，否则保留
                    s.delete(i, i + 1);
                }
            }
        }
    }

    @Click
    void implementBtn() {
        if(!BeidouBoxParams.isBoxConnectNormal){
            toast("未连接蓝牙!");
            return;
        }
        String name = getTextStr(bluetoothName);
        if ("".equals(name)) {
            toast("名称不能为空！");
            return;
        }
        showOldProtocolDialog("是否设置蓝牙名称？", (dialog, confirm) -> {
            if (confirm) {
                TerminalSendManager.getInstance().sendFlyMc(1,name);
            }
            dialog.dismiss();
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(DlYxxInfo dlYxxInfo) {
        toast("设置成功！");
    }

    @Click
    void headLeft() {
        hideInputKeyBord();
        finish();
    }
}
